import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/model/pay/in_app_pay.dart';
import 'package:lcapp/model/toast/toast.dart';

class ChoosePaymentPage extends StatefulWidget {
  final payFun;
  final double total;
  final int id;
  const ChoosePaymentPage({Key? key, this.payFun, required this.total, required this.id}) : super(key: key);

  @override
  _ChoosePaymentPageState createState() => _ChoosePaymentPageState();
}

class _ChoosePaymentPageState extends State<ChoosePaymentPage> {
  /// 支付类型
  PayType payType = PayType.PayType_ALI;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  didPay()async{
    showLoading();
    late ResponseData res;
    if(payType==PayType.PayType_ALI){
      res = await UserAPI.aliPay(widget.id);
    }else if(payType==PayType.PayType_WECHAT){
      res = await UserAPI.wxPay(widget.id);
    }
    hideLoading();
    if(res.code==200){
      SmartDialog.dismiss();
        InAppPay.pay(
            payType,payInfo: res.data);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(child: Container()),
          payTypeContainer(),
          paySub()
        ],
      ),
    );
  }

  /// 支付方式
  Widget payTypeContainer() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(15.w),topRight: Radius.circular(15.w)),
        color: Color(0xFFFfffff),
      ),
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 20.w, top: 20.w),
            child: Text(
              '支付方式',
              style: TextStyle(
                color: Color(0xff333333),
                fontSize: 26.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          Visibility(
            child: payItem(
              img: 'assets/pay/zhifubao.png',
              name: '支付宝支付',
              type: PayType.PayType_ALI,
            ),
          ),
          Visibility(
            child: payItem(
              img: 'assets/pay/weixinzhifu.png',
              name: '微信支付',
              type: PayType.PayType_WECHAT,
            ),
          ),
          //苹果支付
          // Visibility(
          //   child: payItem(
          //     img: 'assets/pay/pingguozhifu.png',
          //     name: '苹果支付',
          //     type: PayType.PayType_APPLE,
          //   ),
          //   visible: false
          // ),
        ],
      ),
    );
  }

  /// 支付方式Item
  Widget payItem({required String img, required String name, required PayType type}) {
    return GestureDetector(
      onTap: () {
        setState(() {
          payType = type;
        });
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 30.w),
        child: Row(
          children: [
            Image.asset(
              img,
              width: 50.w,
              height: 50.w,
            ),
            SizedBox(
              width: 20.w,
            ),
            Expanded(
              child: Container(
                child: Text(
                  name,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 30.sp,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
            payType == type
                ? Container(
              child: Image.asset(
                'assets/pay/choose_paytype.png',
                width: 35.w,
              ),
            )
                : Icon(
              Icons.circle_outlined,
              size: 40.w,
              color: Color(0xff999999),
            ),
          ],
        ),
      ),
    );
  }

  /// 提交
  Widget paySub() {
    return Container(
      padding: EdgeInsets.fromLTRB(
        30.w,
        30.w,
        30.w,
        MediaQuery.of(context).padding.bottom + 20.w,
      ),
      color: Colors.white,
      child: Row(
        children: [
          Expanded(
            child: RichText(
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              text: TextSpan(
                children: <InlineSpan>[
                  TextSpan(
                    text: '合计：' ?? '',
                    style: TextStyle(
                      color: Color(0xFF999999),
                      fontSize: 28.sp,
                    ),
                  ),
                  TextSpan(
                    text: '￥ ',
                    style: TextStyle(
                      color: Color(0xFFFF4F00),
                      fontSize: 26.sp,
                    ),
                  ),
                  TextSpan(
                    text: '${widget.total}',
                    style: TextStyle(
                      color: Color(0xFFFF4F00),
                      fontSize: 36.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              didPay();
            },
            child: Container(
              margin: EdgeInsets.fromLTRB(30.w, 0.w, 0.w, 0.w),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                gradient: LinearGradient(colors: [
                  Color(0xffFF5232),
                  Color(0xffFF8B35),
                ]), // 渐变色
                borderRadius: BorderRadius.circular(35.w),
              ),
              height: 70.w,
              width: 270.w,
              child: Text(
                '立即支付',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 28.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

}
